<template>
  <!-- Dashboard -->
  <div class="page-container">
    <el-form :inline="true" label-width="100px" label-suffix=":">
      <el-row>
        <el-col :span="6">
          <el-form-item label="路口名称">
            <el-input v-model="formData.intersectionName" placeholder="请输入路口名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="发生时间">
            <el-date-picker v-model="formData.occurrenceTime" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <!-- 查询按钮 -->
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-search" @click="fetchData">查询</el-button>
          <el-button plain icon="el-icon-refresh" @click="resetForm">重置</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="$refs.mDialog.open()">新增</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div stylle="height: calc(100vh - 160px)" v-loading="listLoading"> 
      <el-table
        :data="list"
        border
        fit
        highlight-current-row
        style="height: 500px"
      >
        <el-table-column align="center" label="道路名称" prop="roadName"></el-table-column>
        <el-table-column align="center" label="路口名称" prop="intersectionName"></el-table-column>
        <el-table-column align="center" label="事故描述" prop="accidentDescription"></el-table-column>
        <el-table-column align="center" label="伤亡人数" prop="casualties"></el-table-column>
        <el-table-column align="center" label="处理状态" prop="handlingStatus">
          <template slot-scope="scope">
            <!-- 1:已处理,0:未处理 -->
             <el-tag type="success" v-if="scope.row.handlingStatus === 1">已处理</el-tag>
             <el-tag type="warning" v-if="!scope.row.handlingStatus">未处理</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="发生时间" prop="occurrenceTime"></el-table-column>
        <el-table-column align="center" label="发布者" prop="publisherName"></el-table-column>
        <el-table-column align="center" label="操作" width="150">
          <template slot-scope="scope">
            <!-- 删除 -->
             <el-button type="danger" size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
             <!-- 处理 -->
             <el-button type="success" size="mini" @click="handleDeal(scope.$index, scope.row)" v-if="!scope.row.handlingStatus">处理</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 20px; text-align: center">
      <!-- 页码改变和页数改变事件 -->
      <el-pagination
        v-show="list"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
        :current-page="pageInfo.currentPage"
        :page-sizes="[10, 20, 30, 40, 50]"
        style="float: right"
        @current-change="currentPageChange"
        @size-change="sizeChange"
      />
    </div>

    <m-dialog ref="mDialog" @refreshIntersectionData="fetchData" :roadList="roadList"></m-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getTmTrafficAccidentPage, deleteTmTrafficAccident, updateTmTrafficAccident, getTmRoadPage } from "@/api/tm-road";
import mDialog from "./components/dialogIntersection.vue";

export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["name"]),
  },
  components: {
    mDialog
  },
  filters: {
    // 道路类型# 0'主干路', 1'次干路',2 '支路',3 '区间路'
    filterRoadType(value) {
      const roadType = ['主干路', '次干路', '支路', '区间路']
      return roadType[value]
    }
  },
  data() {
    return {
      formData: {
        occurrenceTime: null,
        intersectionName: null
      },
      listLoading: false,
      list: [
      ], // 列表数据
      pageInfo: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
      roadList: []
    };
  },
  mounted() {
    this.fetchData()
    this.fetchRoadData()
  },
  methods: {
    // 获取列表数据
    async fetchData() {
      this.listLoading = true;
      const params = {
        pageNum: this.pageInfo.currentPage,
        pageSize: this.pageInfo.pageSize,
        occurrenceTime: this.formData.occurrenceTime,
        intersectionName: this.formData.intersectionName,
      }
      const { code = undefined, items = {} } = await getTmTrafficAccidentPage({ ...params })
      if(code ===200){
        this.list = items.records;
        this.pageInfo.total = items.total
        setTimeout(() => {
          this.listLoading = false
        }, 200)
      }else{
        this.$message.error('获取数据失败')
        this.listLoading = false
      }
    },
    // 重置表单
    resetForm() {
      this.formData = {
        occurrenceTime: null,
        intersectionName: null
      };
      this.fetchData() 
    },
    // 页码改变事件
    currentPageChange(currentPage) {
      this.pageInfo.currentPage = currentPage;
      this.fetchData()
    },
    // 页数改变事件
    sizeChange(pageSize) {
      this.pageInfo.pageSize = pageSize;
      this.fetchData()
    },
    handleDelete (index, row) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTmTrafficAccident({ id: row.id }).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
      })
    },
    handleDeal (index, row) {
      this.$confirm('是否处理该事故?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateTmTrafficAccident({ id: row.id, handlingStatus: 1 }).then(res => {
          if (res.code === 200) {
            this.$message.success('处理成功')
            this.fetchData()
          } else {
            this.$message.error('处理失败')
          }
        })
      }).catch(() => {
      })
    },
    // 获取道路列表数据
    async fetchRoadData() {
      this.listLoading = true;
      const params = {
        pageNum: this.pageInfo.currentPage,
        pageSize: 1000
      }
      const { code = undefined, items = {} } = await getTmRoadPage({ ...params })
      if(code ===200){
        this.roadList = items.records;
        setTimeout(() => {
          this.listLoading = false
        }, 200)
      }else{
        this.$message.error('获取数据失败')
        this.listLoading = false
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
p {
    margin: 0;
    padding: 0;
}
#clock {
    font-family: 'Share Tech Mono', monospace;
    color: #ffffff;
    text-align: center;
    color: #3F9EFF;
    text-shadow: 0 0 3px yellow;
    .time {
        letter-spacing: 0.05em;
        font-size: 20px;
        padding: 5px 0;
    }
    .date {
        letter-spacing: 0.1em;
        font-size: 18px;
    }
    .text {
        letter-spacing: 0.1em;
        font-size: 12px;
        padding: 20px 0 0;
    }
}
</style>
